package rev3;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Rev3DHTBootstrapMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	       try {
	        	Rev3DHTBootstrapMain dht = new Rev3DHTBootstrapMain();
	        	dht.setupChordFingers();
	        	
	        } catch (Exception e) {
	            System.err.println("NodeMyString exception:");
	            e.printStackTrace();
	        }
	}
	

	NodeMyString n1, n5, n20, n30, n90, n110, n112, n160, n163, n165, n166, n199, n200, n220, n300;
	// Following are the remote references
	NodeMyString rn1, rn5, rn20, rn30, rn90, rn110, rn112, rn160, rn163, rn165, rn166, rn199, rn200, rn220, rn300;
	

	private void setupChordFingers()
	{
		
		try {
			n1 = new NodeMyStringImpl(1);	
			n5 = new NodeMyStringImpl(5);			
			n20 = new NodeMyStringImpl(20);			
			n30 = new NodeMyStringImpl(30);			
			n90 = new NodeMyStringImpl(90);			
			n110 = new NodeMyStringImpl(110);			
			n112 = new NodeMyStringImpl(112);			
			n160 = new NodeMyStringImpl(160);			
			n163 = new NodeMyStringImpl(163);			
			n165 = new NodeMyStringImpl(165);			
			n166 = new NodeMyStringImpl(166);			
			n199 = new NodeMyStringImpl(199);			
			n200 = new NodeMyStringImpl(200);			
			n220 = new NodeMyStringImpl(220);			
			n300 = new NodeMyStringImpl(300);
				
			// Now, pre-seed some data into the map
			MyString ms1 = new MyString("davey", "jones", "1");
			((NodeMyStringImpl)n1).map.put("davey", ms1);
			
			ms1 = new MyString("abraham", "lincoln", "8");
			((NodeMyStringImpl)n5).map.put("abraham", ms1);
	
			ms1 = new MyString("mike", "michalak", "89");
			((NodeMyStringImpl)n30).map.put("mike", ms1);
	
			MyString ms2 = new MyString("dr", "zhivago", "93");
			((NodeMyStringImpl)n90).map.put("dr", ms2);
	
			MyString ms3 = new MyString("barack", "obama", "115");
			((NodeMyStringImpl)n112).map.put("barack", ms3);
				
				
			// Bind the Nodes to the rmi registry
			rn1 = bindNodeToDHT("n1", n1); 
			rn5 = bindNodeToDHT("n5", n5);
			rn20 = bindNodeToDHT("n20", n20);
			rn30 = bindNodeToDHT("n30", n30);
			rn90 = bindNodeToDHT("n90", n90);
			rn110 = bindNodeToDHT("n110", n110);
			rn112 = bindNodeToDHT("n112", n112);
			rn160 = bindNodeToDHT("n160", n160);
			rn163 = bindNodeToDHT("n163", n163);
			rn165 = bindNodeToDHT("n165", n165);
			rn166 = bindNodeToDHT("n166", n166);
			rn199 = bindNodeToDHT("n199", n199);
			rn200 = bindNodeToDHT("n200", n200);
			rn220 = bindNodeToDHT("n220", n220);
			rn300 = bindNodeToDHT("n300", n300);

			n1.addFinger(rn5); n1.addFinger(rn20); n1.addFinger(rn90);  n1.addFinger(rn163); 
			n5.addFinger(rn20); n5.addFinger(rn30); n5.addFinger(rn110);  n5.addFinger(rn165); 
			n20.addFinger(rn30); n20.addFinger(rn90); n20.addFinger(rn112);  n20.addFinger(rn166);
			n30.addFinger(rn90); n30.addFinger(rn110); n30.addFinger(rn160);  n30.addFinger(rn199);
			n90.addFinger(rn110); n90.addFinger(rn112); n90.addFinger(rn163);  n90.addFinger(rn200);
			n110.addFinger(rn112); n110.addFinger(rn160); n110.addFinger(rn165);  n110.addFinger(rn220);
			n112.addFinger(rn160); n112.addFinger(rn163); n112.addFinger(rn166);  n112.addFinger(rn300);
			n160.addFinger(rn163); n160.addFinger(rn165); n160.addFinger(rn199);  n160.addFinger(rn1);
			n163.addFinger(rn165); n163.addFinger(rn165); n163.addFinger(rn200);  n163.addFinger(rn5);
			n165.addFinger(rn166); n165.addFinger(rn199); n165.addFinger(rn1);  n165.addFinger(rn30);

		} catch (RemoteException e) {
			System.out.println("ERROR in test setupChordFingers():");
			e.printStackTrace();
		}
	}
	
	public NodeMyString bindNodeToDHT(String name, NodeMyString n1)
	{
		NodeMyString comp = null;
		try {
            NodeMyString stub = (NodeMyString) UnicastRemoteObject.exportObject(n1, 0);
            
            Registry registry = LocateRegistry.getRegistry();
            //NodeMyString.class.getProtectionDomain().getCodeSource().getLocation();
            registry.rebind(name, stub);
            // Get the remote ref
            comp = (NodeMyString) registry.lookup(name);
            System.out.println("node "+name+" bound");
            
        } catch (Exception e) {
            System.err.println("NodeMyString exception:");
            e.printStackTrace();
        }
        
        return comp;
	}
}
